package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.dao.SportsDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.Sports;
import com.msun.service.SportsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(interfaceClass = SportsService.class)
@Transactional
public class SportsServiceImpl implements SportsService {
    @Autowired
    SportsDao sportsDao;

    @Override
    public void add(List<Sports> list) {

        for (Sports sports : list) {
            Integer id = sports.getId();

            Long count= sportsDao.findById(id);

            if(count > 0){
                sportsDao.edit(sports);
            }else {
                sportsDao.add(sports);
            }
        }

    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Sports> page=   sportsDao.findAll(queryString);
        long total = page.getTotal();
        List<Sports> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void addSports(Sports sports) {
        sportsDao.add(sports);
    }

    @Override
    public List<String> findSportsById(Integer id) {

        List<String> sports= sportsDao.findSportsById(id);
        return sports;
    }

    @Override
    public List<Sports> findAllSports(Integer id) {
        List<Sports> list=sportsDao.findAllSports(id);
        return list;
    }

    @Override
    public List<Integer> findSportsIdsByOrderId(Integer id) {
        List<Integer> list= sportsDao.findSportsIdsByOrderId(id);
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        sportsDao.deleteById(id);
    }

    @Override
    public void edit(Sports sports) {
        sportsDao.edit(sports);
    }
}
